package service

import (
	"gameserver/server"
	"gameserver/server/Gateway/logic"

	"gitee.com/night-tc/gobige/entity"
	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/msgdef"
	"gitee.com/night-tc/gobige/msgdef/protomsg"
)

type Client_Proc struct {
	srv *GatewayService
}

func (this *Client_Proc) MsgProc_ClientVerifyReq(msg *protomsg.ClientVerifyReq) {
	/*
	   用户的验证已结束，现在需要在Service协程上把连接与用户绑定在一起；
	   然后把连接的MsgHandlers换成用户的就可以了；这样连接上收到的消息就会发给用户的协程进行处理了
	*/
	// putil := redisutil.NewGateSession(msg.GetSessUID())
	// info, err := putil.GetInfo(nil)
	// if err != nil {
	// 	return
	// }
	sessmd := this.srv.ClientMgr.GetClientSess(msg.GetSessUID())

	nttmd := this.srv.GetEntityByDBID(msg.GetSessUID(), entity.Entity_Type_Player)
	if nttmd == nil {
		eid := this.srv.GetNewEID()
		var err error
		nttmd, err = this.srv.CreateEntity(entity.Entity_Type_Player, eid, msg.GetSessUID(), false)
		if err != nil {
			logger.Errorf("create pid:%d; err:%v", msg.GetSessUID(), err)
			return
		} else {
			logger.Debugf(server.LogKey_LoginFlow, "create pid:%d; success. ", msg.GetSessUID())
		}
	} else {
		logger.Debugf(server.LogKey_LoginFlow, "reload pid:%d;", msg.GetSessUID())
	}
	nttmd.(entity.IEntityReal).SetClientSess(sessmd)
	if playermd, ok := nttmd.(*logic.Player); ok {
		playermd.CallDelay(func() {
			playermd.ReLoadPlayer(msg.SessState)
		})
	}
}
func (this *Client_Proc) MsgProc_DefaultMsgHandler(msg msgdef.IMsg) {
	if msg.Name() == "HeartBeat" {
		return
	}
	logger.Warnf("Client MsgProc_Default name:%s msg:%s;", msg.Name(), msg)
}
